package presentation;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.leapmotion.leap.Gesture;

import data.LeapMotionSimonSays;

/**
 * This Panel creates the play screen. On the screen you will see the score, which turn it is, and the playfield.
 */

public class PlayPanel extends JPanel {

	private ImageIcon background =
			new ImageIcon("gamebackground1.jpg");
	
	private JLabel scoreLabel, playerLabel, turnLabel, label, imageLabel, gestureIndexLabel;
	private JPanel scorePanel, boardPanel,turnPanel, playerPanel;
	private JButton stopButton;
	private List<String> s = Arrays.asList("Circle_gesture.jpg", "Keytap_gesture.jpg", "Screentap_gesture.jpg", "Swipe_gesture.jpg");
	private static List<Gesture.Type> gestureTypes = Arrays.asList(Gesture.Type.TYPE_CIRCLE , Gesture.Type.TYPE_KEY_TAP, Gesture.Type.TYPE_SCREEN_TAP, Gesture.Type.TYPE_SWIPE);
	// Variable to hold what player's window it is
	private int playerTurn = 1;
	private int score = 0;
	private static LeapMotionSimonSays SiSa;

	private JPanel PlayPanel;
	/**
	 * Create the panel.
	 */
	public PlayPanel() {
		this.setLayout(null);

		// Creation of Panels
		scorePanel = new JPanel();
		boardPanel = new JPanel();
		turnPanel = new JPanel();
		playerPanel = new JPanel();


		// Creation of Labels
		gestureIndexLabel = new JLabel("");
		scoreLabel = new JLabel("Score: " + score);
		playerLabel = new JLabel("Player: " + playerTurn);
		turnLabel = new JLabel("The game: Simon Says");
		label = new JLabel("Motion: ");
		imageLabel = new JLabel(new ImageIcon(s.get(0)));

		// Creation of Button
		stopButton = new JButton("Stop");
		stopButton.setLocation(600, 600);
		stopButton.setSize(120, 30);
		this.add(stopButton);
		
		//If stop is pushed, the game will be stopped.
		stopButton.setActionCommand("Stop");
		PlayPanel = this;
		stopButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent event){ 
				PlayPanel.setVisible(false);
				SiSa = new LeapMotionSimonSays(gestureTypes);
				SiSa.stopGame();
			}
		});

		//Initialize all content
		Initialize();

	}

	private void Initialize(){

		// scorePanel displays the score of all Players
		scoreLabel.setLocation(0, 0);
		scoreLabel.setSize(120, 30);
		scoreLabel.setHorizontalAlignment(0);

		scorePanel.add(scoreLabel);
		scorePanel.setLayout(null);
		scorePanel.setLocation(940, 40);
		scorePanel.setSize(120, 300);
		scorePanel.setBackground(Color.YELLOW);
		PlayPanel.add(scorePanel);

		// Sets size of Board and adds it to playPanel
		imageLabel.setSize(200, 300);
		imageLabel.setLocation(120,50);
		boardPanel.setLayout(null);
		boardPanel.setLocation(420, 40);
		boardPanel.setSize(480, 480);
		
		gestureIndexLabel.setLocation(350, 400);
		gestureIndexLabel.setSize(120, 30);
		boardPanel.add(imageLabel);
		boardPanel.add(gestureIndexLabel);
		PlayPanel.add(boardPanel);


		// Adds turnLabel to turnPanel and sets size
		turnLabel.setLocation(0, 0);
		turnLabel.setSize(150, 30);
		turnLabel.setHorizontalAlignment(0);

		turnPanel.add(turnLabel);
		turnPanel.setLayout(null);
		turnPanel.setSize(150, 30);
		turnPanel.setBackground(Color.GREEN);
		PlayPanel.add(turnPanel);


		// Adds playerLabel to playerPanel and sets size
		playerLabel.setLocation(10, 0);
		playerLabel.setSize(150, 30);

		playerPanel.add(playerLabel);
		playerPanel.setLayout(null);
		playerPanel.setLocation(500, 0);
		playerPanel.setSize(150, 30);
		playerPanel.setBackground(Color.CYAN);
		PlayPanel.add(playerPanel);

	}

	//Set background and size
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(background.getImage(), 0, 0, null);
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(
				background.getImage().getWidth(null),
				background.getImage().getHeight(null));
	}

	
	/* When a new gesture needs to be made, the screen gets updated. 
	*  The player's turn can be updated when neccesary, and his score,
	* but also the image with the gesture needs to be changed.
	*/
	public void changeImage(int gest, int player, int score, boolean newGesture, int indexGesture){
		//Checks if it is a newGesture, so if the pattern will be extended. 
		//Else the image will be a question mark, because the user has to remember the pattern
		if(newGesture){
			boardPanel.remove(imageLabel);
			playerPanel.remove(playerLabel);
			playerTurn = player;
			this.score = score;
			gestureIndexLabel.setVisible(false);
			playerLabel.setText("Player: " + (playerTurn+1));
			scoreLabel.setText("Score: " + this.score);
			imageLabel = new JLabel(new ImageIcon(s.get(gest)));
			Initialize();
		} else{
			boardPanel.remove(imageLabel);
			playerPanel.remove(playerLabel);
			playerTurn = player;
			this.score = score;
			playerLabel.setText("Player: " + (playerTurn+1));
			scoreLabel.setText("Score: " + this.score);
			gestureIndexLabel.setText("Gesture: " + (indexGesture + 1));
			gestureIndexLabel.setVisible(true);
			imageLabel = new JLabel(new ImageIcon("Question_gesture.jpg"));
			Initialize();
		}
	}
}
